//
//  XMShareView.m
//  XMShare
//
//  Created by Amon on 15/8/6.
//  Copyright (c) 2015年 GodPlace. All rights reserved.
//

#import "XMShareView.h"

#import "VerticalUIButton.h"
#import "CommonMarco.h"
#import "XMShareWechatUtil.h"

/** 每一项的高度 */
static const CGFloat itemHeight = 80.0;
/** 水平间隔 */
static const CGFloat itemHorPadding = 60.0;


typedef void(^ShareBlock)(NSString * shareType);

@interface XMShareView ()
/** 分享标题 */
@property (nonatomic, copy) NSString *shareTitle;
/** 分享文本 */
@property (nonatomic, copy) NSString *shareText;
/** 分享链接 */
@property (nonatomic, copy) NSString *shareUrl;
/** 分享图片 */
@property (nonatomic, strong) UIImage *shareImage;

@property (nonatomic, copy) ShareBlock shareBlock;

@end

@implementation XMShareView

#pragma mark - Cycle Methods
+ (XMShareView *)showShareViewWithBlock:(void(^)(NSString * shareType))block{
    XMShareView * shareView = [[XMShareView alloc] initWithFrame:kSTWindow.bounds];
    shareView.shareBlock = block;
    [kSTWindow addSubview:shareView];
    return shareView;
}

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = RGBA(0, 0, 0, 0.4);
        [self configureData];
        [self initUI];
        [self addGes];
    }
    return self;
}

#pragma mark - Myself Methods
- (void)addGes{
    UITapGestureRecognizer * ges = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gesAction)];
    [self addGestureRecognizer:ges];
}

- (void)gesAction{
    [self removeFromSuperview];
}

/** 加载视图 */
- (void)initUI{
    //居中白色视图
    UIView *shareActionView = [[UIView alloc] initWithFrame:CGRectMake(0, self.frame.size.height - 200 - kBottomSafeHeight, self.frame.size.width, 200 + kBottomSafeHeight)];
    shareActionView.backgroundColor = [UIColor whiteColor];
    [self addSubview:shareActionView];
    
    CGFloat width = (shareActionView.frame.size.width - 120 - itemHorPadding) / 2;
    for (int i = 0; i < iconList.count; i++) {
        VerticalUIButton *tempButton;
        UIImage *img = [UIImage imageNamed:iconList[i]];
        
        tempButton = [[VerticalUIButton alloc] initWithFrame:CGRectMake(60 + i * width + i * itemHorPadding, 36, width, itemHeight)];
        tempButton.titleLabel.font = [UIFont systemFontOfSize:15];
        [tempButton setImage:img forState:UIControlStateNormal];
        [tempButton setTitle:textList[i] forState:UIControlStateNormal];
        [tempButton setTitleColor:[UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0 alpha:1.0] forState:UIControlStateNormal];
        [tempButton addTarget:self action:@selector(clickActionButton:) forControlEvents:UIControlEventTouchUpInside];
        if([textList[i] isEqualToString:NSLocalizedString(@"微信好友", nil)]){
            tempButton.tag = SHARE_ITEM_WEIXIN_SESSION;
        }else if([textList[i] isEqualToString:NSLocalizedString(@"微信朋友圈", nil)]){
            tempButton.tag = SHARE_ITEM_WEIXIN_TIMELINE;
        }
        [shareActionView addSubview:tempButton];
    }
    
    UIButton * cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    cancelBtn.frame = CGRectMake(15, shareActionView.frame.size.height - kBottomSafeHeight - 15 - 44, shareActionView.frame.size.width - 30, 44);
    cancelBtn.backgroundColor = [UIColor colorWithRed:244/255.0 green:244/255.0 blue:244/255.0 alpha:1.0];
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancelBtn setTitleColor:[UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1.0] forState:UIControlStateNormal];
    cancelBtn.titleLabel.font = [UIFont systemFontOfSize:17];
    [cancelBtn addTarget:self action:@selector(hiddenShareView) forControlEvents:UIControlEventTouchUpInside];
    ViewRadius(cancelBtn, 4);
    [shareActionView addSubview:cancelBtn];
}

- (void)hiddenShareView{
    [self removeFromSuperview];
}

/** 初始化数据 */
- (void)configureData{
    iconList = [[NSMutableArray alloc] init];
    textList = [[NSMutableArray alloc] init];
    
    [iconList addObject:@"icon_share_wechat@2x"];
    [textList addObject:NSLocalizedString(@"微信好友", nil)];
    
    [iconList addObject:@"icon_share_moment@2x"];
    [textList addObject:NSLocalizedString(@"微信朋友圈", nil)];
}

- (void)clickActionButton:(VerticalUIButton *)sender{
    if (sender.tag == 0) {
        self.shareBlock(@"WechatMessage");
    } else {
        self.shareBlock(@"WechatTimeline");
    }
    [self removeFromSuperview];
}

+ (void)shareWithTitle:(NSString *)title content:(NSString *)content url:(NSString *)url image:(UIImage *)image type:(SHARE_ITEM)type{
    if (type == SHARE_ITEM_WEIXIN_SESSION) {
        //微信好友
        [self shareToWeixinSessionWithTitle:title content:content url:url image:image];
    } else {
        //朋友圈
        [self shareToWeixinTimelineWithTitle:title content:content url:url image:image];
    }
}

+ (void)shareToWeixinSessionWithTitle:(NSString *)title content:(NSString *)content url:(NSString *)url image:(UIImage *)image{
    BOOL hadInstalledWeixin = [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"weixin://"]];
    if (!hadInstalledWeixin) {
        [kSTWindow makeToast:@"未安装微信客户端！"];
        return;
    }
    XMShareWechatUtil *util = [XMShareWechatUtil sharedInstance];
    util.shareTitle = title;
    util.shareText = content;
    util.shareUrl = url;
    util.shareImage = image;
    [util shareToWeixinSession];
}

+ (void)shareToWeixinTimelineWithTitle:(NSString *)title content:(NSString *)content url:(NSString *)url image:(UIImage *)image{
    BOOL hadInstalledWeixin = [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"weixin://"]];
    if (!hadInstalledWeixin) {
        [kSTWindow makeToast:@"未安装微信客户端！"];
        return;
    }
    XMShareWechatUtil *util = [XMShareWechatUtil sharedInstance];
    util.shareTitle = title;
    util.shareText = content;
    util.shareUrl = url;
    util.shareImage = image;
    [util shareToWeixinTimeline];
}
@end
